<!-- 横版大图，商家卡片 -->
<template>
	<view class="big-goods u-flex u-col-top u-m-b-16" @tap="click" style="position: relative">
		<image class="goods-img" lazy-load fade-show :src="image" mode="aspectFill"></image>
		<view class="discount-box">
			{{detail.discount + '折'}}
		</view>
		<view class=" card-right u-flex-col u-row-between u-m-r-10 u-m-l-10">
			<view class="">
				<view class="goods-title u-ellipsis-1 u-m-t-10 u-m-b-10">
					{{ title }}
				</view>
			</view>
			<view class="u-m-b-10 rongqis  u-flex" v-if="shop_tags.length">
				<view class="line-red" v-for="(item, index) in shop_tags" :key="index">{{ item }}</view>
			</view>
			<view class="origin-price">{{ shop_text }}</view>
			<view class=" u-flex u-row-between u-col-center">
				<view class="star-box">
					<u-rate v-model="shop_score" inactive-color='#E2DFDD' active-color="#FE811C" :disabled="true">
					</u-rate>
				</view>

				<!--				&lt;!&ndash; 加入购物车 &ndash;&gt;-->
				<!--				<view class="cart-box" v-if="!detail.is_sku">-->
				<!--					&lt;!&ndash; 单规格 &ndash;&gt;-->
				<!--					<view class="" v-if="!detail.is_sku">-->
				<!--&lt;!&ndash;						<button&ndash;&gt;-->
				<!--&lt;!&ndash;							v-if="!isCart(detail.id)"&ndash;&gt;-->
				<!--&lt;!&ndash;							@tap.stop="addCart(detail.sku_price[0])"&ndash;&gt;-->
				<!--&lt;!&ndash;							class="u-reset-button buy-btn u-flex u-row-center u-col-center"&ndash;&gt;-->
				<!--&lt;!&ndash;							:style="type ? typeMap[type].btnBg : ''"&ndash;&gt;-->
				<!--&lt;!&ndash;						>&ndash;&gt;-->
				<!--&lt;!&ndash;              <view class="u-iconfont uicon-shopping-cart-fill" style="color: #fff;"></view>&ndash;&gt;-->
				<!--&lt;!&ndash;						</button v-else>&ndash;&gt;-->
				<!--						<view class="num-step">-->
				<!--&lt;!&ndash;							<u-number-box&ndash;&gt;-->
				<!--&lt;!&ndash;								:value="checkCart[detail.id].num"&ndash;&gt;-->
				<!--&lt;!&ndash;								:min="0"&ndash;&gt;-->
				<!--&lt;!&ndash;								:step="1"&ndash;&gt;-->
				<!--&lt;!&ndash;								:max="detail.sku_price[0].stock"&ndash;&gt;-->
				<!--&lt;!&ndash;								disabled-input&ndash;&gt;-->
				<!--&lt;!&ndash;								@min="onMin"&ndash;&gt;-->
				<!--&lt;!&ndash;								@plus="plus($event, detail.sku_price[0])"&ndash;&gt;-->
				<!--&lt;!&ndash;								@change="onChangeNum($event, detail.sku_price[0])"&ndash;&gt;-->
				<!--&lt;!&ndash;							></u-number-box>&ndash;&gt;-->
				<!--              <button-->
				<!--                  @tap.stop="onChangeNum('1', detail.sku_price[0])"-->
				<!--                  class="u-reset-button item-btn cart-btn  u-flex u-col-center u-row-center"-->
				<!--                  :style="type ? typeMap[type].btnBg : ''"-->
				<!--              >-->
				<!--                <view class="u-iconfont uicon-shopping-cart-fill" style="color: #fff;"></view>-->
				<!--              </button>-->
				<!--						</view>-->
				<!--					</view>-->
				<!--					&lt;!&ndash; 多规格 &ndash;&gt;-->
				<!--					<button class="u-reset-button item-btn buy-btn" :style="type ? typeMap[type].btnBg : ''" @tap.stop="selSku(detail)" v-else>-->
				<!--            <view class="u-iconfont uicon-shopping-cart-fill" style="color: #fff;"></view>-->
				<!--					</button>-->
				<!--				</view>-->
			</view>
		</view>
		<u-toast ref="submitAudit"></u-toast>
		<!-- 规格弹窗 -->
		<!--		<shopro-sku v-if="showSku && goodsInfo.id" v-model="showSku" :goodsInfo="goodsInfo" buyType="cart"></shopro-sku>-->
	</view>
</template>

<script>
	/**
	 *shoproGoodsCard - 商品列表卡片
	 * @property {Object} detail - 商品详情
	 * @property {String} type - 商品类型
	 * @property {String} image - 商品图片
	 * @property {String} title - 商品标题
	 * @property {String} subtitle - 商品副标题
	 * @property {String | Number} price - 商品价格
	 * @property {String | Number} originPrice - 商品原价
	 * @property {String | Number} sales - 商品销量
	 * @property {Array} tagTextList - 活动标签
	 * @event {Function} click 商品被点击
	 */
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	export default {
		components: {},
		data() {
			return {
				showSku: false,
				goodsInfo: {}, //商品详情
			};
		},
		computed: {
			...mapGetters(['cartList', 'checkCart'])
		},
		props: {
			detail: {
				type: Object,
				default: () => {
					return {};
				}
			},
			type: {
				type: [String, null],
				default: ''
			},
			image: {
				type: String,
				default: ''
			},
			title: {
				type: String,
				default: ''
			},
			shop_text: {
				type: [String, Number],
				default: ''
			},
			shop_tags: {
				type: Array,
				default: () => []
			},
			store_shop_id: {
				type: [String, Number],
				default: 0
			},
			shop_score: {
				type: [String, Number],
				default: 0
			}
		},
		mounted() {},
		methods: {
			...mapActions(['getCartList', 'changeCartList', 'addCartGoods']),
			//点击商品
			click() {
				this.$emit('click');
			},

			// 检测是否为购物车商品
			isCart(id) {
				return Object.keys(this.checkCart).includes(id + '');
			},

			// 检测商品在购物车中的下标
			checkGoodsIndex(id) {
				let cIndex = 0;
				this.cartList.forEach((item, index) => {
					if (id == item.goods_id) {
						cIndex = index;
					}
				});
				return cIndex;
			},

			// 更改商品数
			async onChangeNum(e, sku) {
				let gIndex = this.checkGoodsIndex(sku.goods_id);
				console.log("==============gindex", gIndex, this.checkCart[sku.goods_id].num)
				// if (e != this.checkCart[sku.goods_id].num) {
				// uni.showLoading({
				// 	mask: true
				// });
				this.$set(this.cartList[gIndex], 'goods_num', +e);
				await this.changeCartList({
					ids: [this.checkCart[sku.goods_id].cartOrderId],
					goodsNum: +e,
					art: 'change'
				});
				let options = {
					type: "default",
					icon: 'checkmark-circle',
					back: false,
					title: '成功加入购物车'
				};
				this.$refs.submitAudit.show(options);
				// await uni.hideLoading();
				// }
			},

			// 到达最小值
			onMin() {
				const that = this;
				let cartGoodId = 0;
				cartGoodId = this.cartList.filter(item => item.goods_id === that.detail.id)[0].id;
				uni.showModal({
					title: '删除提示',
					confirmColor: '#f0c785',
					content: `是否确认从购物车中删除此商品?`,
					success: res => {
						res.confirm && this.changeCartList({
							ids: [cartGoodId],
							art: 'delete'
						});
					}
				});
			},

			// 增加
			plus(e, sku) {
				if (e.value >= sku.stock) {
					this.$u.toast('库存不足');
					return;
				}
				if (this.detail.activity_type === 'seckill' || this.detail.activity_type === 'groupon') {
					let rules = this.detail.activity.rules;
					if (rules.limit_buy != 0 && e.value >= rules.limit_buy) {
						this.$u.toast('本次活动最多购买' + rules.limit_buy + '件');
						return;
					}
				}
			},

			// 添加购物车,多规格
			async selSku(info) {
				let options = {
					type: "default",
					icon: 'checkmark-circle',
					back: false,
					title: '成功加入购物车'
				};
				this.$refs.submitAudit.show(options);

				if (this.detail.activity_type) {
					this.$Router.push({
						path: '/pages/goods/new_detail',
						query: {
							id: this.detail.id
						}
					});
					return;
				}
				this.goodsInfo = {};
				this.getGoodsDetail(info.id);
				this.showSku = true;
			},

			// 商品详情
			getGoodsDetail(id) {
				let that = this;
				that.$http('goods.detail', {
					id: id
				}).then(res => {
					if (res.code === 1) {
						that.goodsInfo = res.data;
					}
				});
			},

			// 加入购物车
			addCart(sku) {
				if (sku.stock <= 0) {
					this.$u.toast('库存不足');
					return;
				}
				let options = {
					type: "default",
					icon: 'checkmark-circle',
					back: false,
					title: '成功加入购物车'
				};
				this.$refs.submitAudit.show(options);

				// if (this.detail.activity_type) {
				// 	this.$Router.push({ path: '/pages/goods/new_detail', query: { id: this.detail.id } });
				// 	return;
				// }
				// let confirmGoodsList = {
				// 	list: [
				// 		{
				// 			goods_id: sku.goods_id,
				// 			goods_num: 1,
				// 			sku_price_id: sku.id,
				// 			goods_price: sku.price
				// 		}
				// 	],
				// 	from: 'goods'
				// };
				// this.addCartGoods(confirmGoodsList).then(res => {
				// 	if (res.code === 1) {
				// 		this.$u.toast(res.msg);
				// 	}
				// });
			}
		}
	};
</script>

<style lang="scss">
	// 大商品卡片
	.big-goods {
		width: 100%;
		min-height: 460rpx;
		background: #ffffff;
		border-radius: 20rpx;
		//background-repeat: no-repeat;
		//background-size: 100% 100%;
		display: flex;
		flex-direction: column;

		.goods-img {
			width: 100%;
			height: 260rpx;
			background-color: #f5f5f5;
			border-radius: 20rpx;
		}

		.card-right {
			width: 100%;
			//height: 220rpx;
			margin: 16rpx 0;

		}

		.goods-title {
			font-size: 26rpx;
			font-weight: 600;
			width: 100%;
			color: #000000;
			padding-top: 6rpx;
			overflow: hidden; // 溢出隐藏
			white-space: nowrap; // 强制一行
			text-overflow: ellipsis; // 文字溢出显示省略号

			.title-tag {
				transform: scale(0.9);
				position: relative;
				top: -6rpx;
				font-size: 36rpx;
			}
		}

		.subtitle-text {
			font-size: 22rpx;
			width: 100%;
			font-weight: 500;
			color: #666666;
			overflow: hidden; // 溢出隐藏
			white-space: nowrap; // 强制一行
			text-overflow: ellipsis; // 文字溢出显示省略号
		}

		.tag-box {
			border: 1rpx solid #ff0000;
			display: inline-block;
			font-size: 20rpx;
			line-height: 30rpx;
			padding: 0 10rpx;
			color: #ff0000;
			border-radius: 8rpx;
		}

		// 购物车
		.cart-box {
			.cart-btn {
				width: 54rpx;
				height: 54rpx;
				border-radius: 50%;
				padding: 0;
				background: rgb(252, 83, 81);
				//background: linear-gradient(90deg, #e9b461, #eecc89);
			}

			.buy-btn {
				width: 120rpx;
				line-height: 50rpx;
				background: rgb(252, 83, 81);
				//background: linear-gradient(90deg, #e9b461, #eecc89);
				border-radius: 25rpx;
				font-size: 24rpx;
				font-weight: 500;
				color: #ffffff;
			}
		}

		// 价格
		.price {
			color: #ff0000;
			font-weight: 600;
		}

		.origin-price {
			color: #c4c4c4;
			font-size: 24rpx;
		}
	}

	.rongqis {
		flex-wrap: wrap;
	}

	.line-red {
		padding: 4rpx 8rpx;
		margin-bottom: 10rpx;
		line-height: 30rpx;
		background-color: #FFF0E4;
		text-align: center;
		font-size: 20rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		margin-right: 6rpx;
		color: #FE7F1A !important;
		border: none !important;
	}

	.discount-box {
		position: absolute;
		left: 30rpx;
		top: 195rpx;
		width: 74rpx;
		height: 36rpx;
		background: linear-gradient(270deg, #FF5251 0%, #FD8E8A 100%);
		box-shadow: 0rpx 4rpx 4rpx 2rpx rgba(214, 53, 53, 0.3300);
		border-radius: 16rpx 0rpx;
		font-size: 10px;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
		text-align: center;
		line-height: 36rpx;
	}
</style>
